package cn.stylefeng.guns.modular.job;


import cn.stylefeng.guns.modular.app.dto.GroupMiningConfigDTO;
import cn.stylefeng.guns.modular.app.dto.GroupMiningDTO;
import cn.stylefeng.guns.modular.app.service.MemberService;
import cn.stylefeng.guns.modular.app.service.TeamRewardsService;
import cn.stylefeng.guns.modular.base.state.Constant;

import cn.stylefeng.guns.modular.base.state.F;
import cn.stylefeng.guns.modular.fin.entity.Currency;
import cn.stylefeng.guns.modular.fin.service.CurrencyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


// 社区每周奖励发放
@Component
@Slf4j
public class TeamRecordJob extends Constant {
    @Autowired
    MemberService memberService;
    @Autowired
    private CurrencyService currencyService;

    public static List<GroupMiningConfigDTO> groupMiningConfigDTOList =  new ArrayList<>(Arrays.asList(
            new GroupMiningConfigDTO("V1",BigDecimal.valueOf(20000),BigDecimal.valueOf(10000),0.0005F), // 添加第一个默认对象
            new GroupMiningConfigDTO("V2",BigDecimal.valueOf(100000),BigDecimal.valueOf(50000),0.0006F), // 添加第二个默认对象
            new GroupMiningConfigDTO("V3",BigDecimal.valueOf(200000),BigDecimal.valueOf(100000),0.0007F), // 添加第三个默认对象
            new GroupMiningConfigDTO("V3",BigDecimal.valueOf(400000),BigDecimal.valueOf(200000),0.0008F), // 添加第三个默认对象
            new GroupMiningConfigDTO("V4",BigDecimal.valueOf(1000000),BigDecimal.valueOf(500000),0.0009F), // 添加第四个默认对象
            new GroupMiningConfigDTO("V5",BigDecimal.valueOf(2000000),BigDecimal.valueOf(1000000),0.001f)  // 添加第五个默认对象
    ));


    @Scheduled(cron = "0 0 1 * * ?")
//    @PostConstruct
    public void checkTeamRecord() {
        List<Long> miningUserList = memberService.getMiningUserList();
        LocalDate startDate = LocalDate.now().plusDays(-1);
        for (Long memberId : miningUserList) {
            //获取每个下级的质押BTB总量
            List<GroupMiningDTO> myGoupMining = memberService.getMyGoupMining(memberId,startDate);

            BigDecimal bigGroupMining =BigDecimal.ZERO;
            BigDecimal smallGroupMining=BigDecimal.ZERO;

            for (GroupMiningDTO groupMiningDTO : myGoupMining) {
                //bi最大的大
                    if(groupMiningDTO.getReleasePledgeNum().compareTo(bigGroupMining) >0){
                        smallGroupMining = smallGroupMining.add(bigGroupMining);
                        bigGroupMining = groupMiningDTO.getReleasePledgeNum();
                    }else {
                        smallGroupMining = smallGroupMining.add(groupMiningDTO.getReleasePledgeNum());
                    }
            }

            BigDecimal finalBigGroupMining = bigGroupMining;
            BigDecimal finalSmallGroupMining = smallGroupMining;
            List<GroupMiningConfigDTO> collect = groupMiningConfigDTOList.stream().filter(f -> f.getBigGroupMining().compareTo(finalBigGroupMining) < 1
                    && f.getSmallGroupMining().compareTo(finalSmallGroupMining) < 1).sorted(Comparator.comparing(GroupMiningConfigDTO::getRate).reversed()).collect(Collectors.toList());


            if(collect.isEmpty()){
                continue;
            }
            GroupMiningConfigDTO groupMiningConfigDTO = collect.get(0);

            Currency currency = F.me().getCurrency(memberId, "BTB");
           BigDecimal shifang = currency.getReleasePledgeNum().multiply(BigDecimal.valueOf(groupMiningConfigDTO.getRate())).setScale(2, RoundingMode.HALF_UP);
           currencyService.releaseCurrencyReleasePledgeNum(memberId,currency.getType(),shifang,"社区奖励");

        }
    }

}
